package financial.sharing.center.receivable.service.impl;

import com.github.pagehelper.PageInfo;
import financial.sharing.center.common.enums.AccruedOperationStageEnum;
import financial.sharing.center.common.enums.EnableStatusEnum;
import financial.sharing.center.common.enums.ResponseContext;
import financial.sharing.center.common.utils.BigDecimalUtil;
import financial.sharing.center.receivable.dao.ReceivableAccruedMapper;
import financial.sharing.center.receivable.model.entity.ReceivableAccrued;
import financial.sharing.center.receivable.service.ReceivableAccruedService;
import financial.sharing.center.receivable.service.ReceivableBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReceivableAccruedServiceImpl
 * @Description TODO
 * @Author guoyuhang
 * @ @csdn https://blog.csdn.net/qq_40788718
 * @Date 2021/5/18
 * @Version 1.0
 */
@Service
public class ReceivableAccruedServiceImpl implements ReceivableAccruedService {

    @Autowired
    private ReceivableAccruedMapper receivableAccruedMapper;

    @Autowired
    private ReceivableBillService receivableBillServiceImpl;

    @Override
    public ReceivableAccrued getReceivableAccrued(Integer id){
        return this.receivableAccruedMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageInfo<ReceivableAccrued> getAllReceivableAccrued(Integer contractId, Integer accruedId) {
        Example example = new Example(ReceivableAccrued.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue());
        if (contractId != null && contractId >= 0){
            criteria.andEqualTo("contractId", contractId);
        }
        if (accruedId != null && accruedId >= 0){
            criteria.andEqualTo("id", accruedId);
        }
        List<ReceivableAccrued> receivableAccruedList = this.receivableAccruedMapper.selectByExample(example);
        for (ReceivableAccrued receivableAccrued : receivableAccruedList) {
            receivableAccrued.setBillNumber(this.receivableBillServiceImpl.getBillNumber(receivableAccrued));
            receivableAccrued.setAccruedOperationStageName(AccruedOperationStageEnum.int2Enum(receivableAccrued.getAccruedOperationStageValue()).getName());
        }
        return new PageInfo<>(receivableAccruedList);
    }

    @Override
    public Integer createOneReceivableAccrued(BigDecimal settlementAmount) {
        ReceivableAccrued receivableAccrued = new ReceivableAccrued(settlementAmount);
        int i = this.receivableAccruedMapper.insert(receivableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.创建失败.getValue();
    }

    @Override
    public Integer compileOneReceivableAccrued(ReceivableAccrued receivableAccrued) {
        int i = this.receivableAccruedMapper.updateByPrimaryKey(receivableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.更新失败.getValue();
    }

    @Override
    public Integer deleteOneReceivableAccrued(Integer id) {
        ReceivableAccrued receivableAccrued = this.getReceivableAccrued(id);
        receivableAccrued.setEnableStatusValue(EnableStatusEnum.弃用.getValue());
        int i = this.receivableAccruedMapper.updateByPrimaryKey(receivableAccrued);
        return i == 1 ? ResponseContext.成功.getValue() : ResponseContext.删除失败.getValue();
    }

    @Override
    public Map<Integer, BigDecimal> getMapReceivableAccrued() {
        Example example = new Example(ReceivableAccrued.class);
        example.createCriteria().andEqualTo("enableStatusValue", EnableStatusEnum.正常.getValue())
                .andLessThanOrEqualTo("accruedOperationStageValue", AccruedOperationStageEnum.已审核.getValue());
        List<ReceivableAccrued> receivableAccruedList = this.receivableAccruedMapper.selectByExample(example);
        Map<Integer,BigDecimal> result = new HashMap<>();
        for (ReceivableAccrued receivableAccrued : receivableAccruedList) {
            if (receivableAccrued.getContractId() != null){
                if (result.get(receivableAccrued.getContractId()) == null){
                    result.put(receivableAccrued.getContractId(), BigDecimalUtil.ZERO);
                }
                result.put(receivableAccrued.getContractId(),
                        result.get(receivableAccrued.getContractId()).add(receivableAccrued.getSettlementAmount()));
            }
        }
        return result;
    }
}
